class Solution {
    public int maxOperations(int[] nums, int k) {
        Arrays.sort(nums);
        int n=nums.length;
        int p=0;
        int q=n-1;
        int res=0;
        while(p<q)
        {
            int tar=nums[q]+nums[p];
            if(tar==k)
            {
                p++;
                q--;
                res++;
            }
            if(tar>k)
            {
                q--;
            }
            if(tar<k)
            {
                p++;
            }
        }
        return res;
    }
}
